package com.ln.lnzw.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.flyco.tablayout.SlidingTabLayout;
import com.ln.lnzw.R;
import com.ln.lnzw.adapter.NewsFragmentPagerAdapter;
import com.ln.lnzw.app.AppConstant;
import com.ln.lnzw.base.BaseFragment;
import com.ln.lnzw.utils.StatusBarUtils;
import com.ln.lnzw.view.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by wuwei on 2018/3/21.
 * 公开
 */

public class PublicityFragment extends BaseFragment {
    @BindView(R.id.SlidingTabLayout)
    SlidingTabLayout SlidingTabLayout;
    @BindView(R.id.viewpager)
    CustomViewPager viewpager;
    Unbinder unbinder;
    @BindView(R.id.fake_status_bar)
    ImageView fakeStatusBar;
    private List<Fragment> fragmentList;
    private List<String> nameFragment;
    private NewsFragmentPagerAdapter mAdapetr;
    private AnnouncementFragment announcementFragment;

    public static PublicityFragment newInstance() {
        Bundle args = new Bundle();
        PublicityFragment fragment = new PublicityFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.publicityfragment_activity, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        StatusBarUtils.statusBar(fakeStatusBar);
        initFragment();
    }

    private void initView() {
        mAdapetr = new NewsFragmentPagerAdapter(activity.getSupportFragmentManager(), fragmentList, nameFragment);
        viewpager.setAdapter(mAdapetr);
        viewpager.setOffscreenPageLimit(2);
        viewpager.setCurrentItem(0, false);
        SlidingTabLayout.setViewPager(viewpager);

    }

    private void initFragment() {
        nameFragment = new ArrayList<>();
        fragmentList = new ArrayList<>();
        nameFragment.add("通知公告");
        nameFragment.add("工作动态");
        nameFragment.add("政策法规");

//        WorkDynamicsFragment workDynamicsFragment = new WorkDynamicsFragment();
//        PoliciesRegulationsFragment policiesRegulationsFragment = new PoliciesRegulationsFragment();
//        fragmentList.add(announcementFragment);
//        fragmentList.add(workDynamicsFragment);
//        fragmentList.add(policiesRegulationsFragment);

        for (int i = 0; i < 3; i++) {
            if (i == 0) {
                announcementFragment = new AnnouncementFragment();
                Bundle b = new Bundle();
                b.putString("url", AppConstant.ANNOUNCEMENT_URL);
                b.putString("moreUrl", AppConstant.ANNOUNCEMENT_MORE_URL);
                b.putString("name", nameFragment.get(0));
                announcementFragment.setArguments(b);
                fragmentList.add(announcementFragment);
            } else if (i == 1) {
                announcementFragment = new AnnouncementFragment();
                Bundle b = new Bundle();
                b.putString("url", AppConstant.WORKDYNAMICS_URL);
                b.putString("moreUrl", AppConstant.WORKDYNAMICS_MORE_URL);
                b.putString("name", nameFragment.get(1));
                announcementFragment.setArguments(b);
                fragmentList.add(announcementFragment);
            } else if (i == 2) {
                announcementFragment = new AnnouncementFragment();
                Bundle b = new Bundle();
                b.putString("url", AppConstant.POLICIESREGULATIONS_URL);
                b.putString("moreUrl", AppConstant.POLICIESREGULATIONS_MORE_URL);
                b.putString("name", nameFragment.get(2));
                announcementFragment.setArguments(b);
                fragmentList.add(announcementFragment);
            }
        }
        initView();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
